package com.assignwork.service.impl;

import com.assignwork.dto.AssignWorkDTO;
import com.assignwork.dto.WorkArrangeOtherSearchDTO;
import com.assignwork.dto.WorkArrangeOtherSubmitDTO;
import com.assignwork.entity.WorkArrangeOther;
import com.assignwork.mapper.WorkArrangeOtherMapper;
import com.assignwork.service.IWorkArrangeOtherService;
import com.assignwork.service.IWorkArrangeWorkersService;
import com.assignwork.vo.WorkArrangeOtherPageVO;
import com.assignwork.vo.WorkArrangeOtherVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.cache.DictBizCache;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName WorkArrangeOtherServiceImpl
 * @Description 其他配工作业接口实现
 * @Author dujianyu
 * @Date 2023/12/11 8:01
 */
@Service
@AllArgsConstructor
public class WorkArrangeOtherServiceImpl extends BaseServiceImpl<WorkArrangeOtherMapper, WorkArrangeOther> implements IWorkArrangeOtherService {

    private static final String ARRANGE_TYPE = "OTHER";
    private IWorkArrangeWorkersService workersService;

    @Override
    public Boolean submitWaOther(WorkArrangeOtherSubmitDTO dto) {
        WorkArrangeOther entity = BeanUtil.copy(dto, WorkArrangeOther.class);
        super.saveOrUpdate(entity);
        if (entity != null) {
            return workersService.submitWaWorkers(entity.getId(), dto.getWorkers(), ARRANGE_TYPE);
        }
        return true;
    }

    @Override
    public IPage<WorkArrangeOtherPageVO> selectWaOtherPage(WorkArrangeOtherSearchDTO dto, IPage<WorkArrangeOtherPageVO> page) {
        List<WorkArrangeOtherPageVO> data = baseMapper.selectWaOtherPage(dto, page);
        return page.setRecords(dataProcess(data));
    }

    @Override
    public boolean removeWaOther(Long id) {
        return super.deleteLogic(Func.toLongList(id.toString())) && workersService.removeByArrangeId(id);
    }

    @Override
    public IPage<WorkArrangeOtherVO> pageWorkArrangeOther(BladeUser bladeUser, IPage<WorkArrangeOtherVO> page, AssignWorkDTO dto) {
        List<WorkArrangeOtherVO> res = baseMapper.pageWorkArrangeOther(page, dto);
        for (WorkArrangeOtherVO vo : res) {
            vo.setTimeSpaceName(DictBizCache.getValue("time_space", vo.getTimeSpace()));
            vo.setOtherWorkTypeName(DictBizCache.getValue("other_work_type", vo.getOtherWorkType()));
        }
        page.setRecords(res);
        return page;
    }

    private List<WorkArrangeOtherPageVO> dataProcess(List<WorkArrangeOtherPageVO> data) {
        for (WorkArrangeOtherPageVO vo : data) {
            vo.setTimeSpaceName(DictBizCache.getValue("time_space", vo.getTimeSpace()));
            vo.setOtherWorkTypeName(DictBizCache.getValue("other_work_type", vo.getOtherWorkType()));
        }
        return data;
    }
}
